package com.daxt.mapper.sys;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.daxt.model.service.base.vo.Dept;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DeptMapper extends BaseMapper<Dept> {

    @Select("SELECT T2.*\n" +
            "FROM ( \n" +
            "    SELECT \n" +
            "        @r AS _id, \n" +
            "        (SELECT @r := parentId FROM base_dept WHERE id = _id) AS parentId, \n" +
            "        @l := @l + 1 AS lvl \n" +
            "    FROM \n" +
            "        (SELECT @r := #{deptId}, @l := 0) vars, \n" +
            "        base_dept h \n" +
            "    WHERE @r <> 0) T1 \n" +
            "JOIN base_dept T2 \n" +
            "ON T1._id = T2.id \n" +
            "ORDER BY T1.lvl DESC ")
    List<Dept> getAllParentDept(long deptId);
}